package com.project.movie.controller.account;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.domain.MovieAccount;
import com.project.movie.domain.Result;
import com.project.movie.service.MovieAccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 公告controller
 * @date 2025/02/06 11:38
 */
@Controller
@ResponseBody
@RequestMapping("account")
public class MovieAccountController {

    @Autowired
    private MovieAccountService movieAccountService;

    /** 分页获取公告 */
    @PostMapping("getMovieAccountPage")
    public Result getMovieAccountPage(@RequestBody MovieAccount movieAccount) {
        Page<MovieAccount> page = new Page<>(movieAccount.getPageNumber(),movieAccount.getPageSize());
        QueryWrapper<MovieAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(movieAccount.getContent()),MovieAccount::getContent,movieAccount.getContent());
        Page<MovieAccount> movieAccountPage = movieAccountService.page(page, queryWrapper);
        return Result.success(movieAccountPage);
    }

    @GetMapping("getMovieAccountList")
    public Result getMovieAccountList() {
        QueryWrapper<MovieAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(MovieAccount::getId).last("limit 20");
        List<MovieAccount> accountList = movieAccountService.list(queryWrapper);
        return Result.success(accountList);
    }

    /** 根据id获取公告 */
    @GetMapping("getMovieAccountById")
    public Result getMovieAccountById(@RequestParam("id")String id) {
        MovieAccount movieAccount = movieAccountService.getById(id);
        return Result.success(movieAccount);
    }

    /** 保存公告 */
    @PostMapping("saveMovieAccount")
    public Result saveMovieAccount(@RequestBody MovieAccount movieAccount) {
        boolean save = movieAccountService.save(movieAccount);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑公告 */
    @PostMapping("editMovieAccount")
    public Result editMovieAccount(@RequestBody MovieAccount movieAccount) {
        boolean save = movieAccountService.updateById(movieAccount);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除公告 */
    @GetMapping("removeMovieAccount")
    public Result removeMovieAccount(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                movieAccountService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("公告id不能为空！");
        }
    }

}